﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WebNext.Domain.Entities
{
	public class Entry : Entity
	{
		public const int MaxTitleLength = 200;

		public virtual string Title
		{
			get;
			set;
		}

		public virtual IList<EntryElement> Content
		{
			get;
			set;
		}

		public virtual IList<Comment> Comments
		{
			get;
			protected set;
		}

		public virtual int Rank
		{
			get;
			set;
		}

		public virtual bool IsPublished
		{
			get;
			set;
		}

		public virtual bool CommentsEnabled
		{
			get;
			set;
		}

		public virtual EntryType Type
		{
			get;
			set;
		}

		public virtual CultureId CultureId
		{
			get;
			set;
		}

		public virtual IList<Tag> Tags
		{
			get;
			private set;
		}

		protected Entry()
		{
			this.IsPublished = false;
			this.CommentsEnabled = true;
			
			this.Content = new List<EntryElement>();
			this.Comments = new List<Comment>();
			this.Tags = new List<Tag>();
		}

		public Entry(string title) : this()
		{
			this.Title = title;
		}

		public virtual bool ContainsText(string text)
		{
			if (this.TitleContainsText(text))
			{
				return true;
			}
			
			foreach(EntryElement element in this.Content)
			{
				if (element.ContainsText(text))
				{
					return true;
				}
			}
			return false;
		}

		private bool TitleContainsText(string text)
		{
			return this.Title != null && this.Title.IndexOf(text, StringComparison.InvariantCultureIgnoreCase) >= 0;
		}
	}
}
